package com.szms.mobile;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.szms.mobile.check.PhotoFragment;
import com.szms.mobile.listeners.AddFragmentListener;
import com.szms.mobile.me.LoginFragment;
import com.szms.mobile.me.MeFragment;
import com.szms.mobile.me.SystemFragment;
import com.szms.mobile.util.MenuUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MenuUtils.Instance().getMap().put(R.id.layoutme, Arrays.asList(MeFragment.class,LoginFragment.class,SystemFragment.class));
        MenuUtils.Instance().getMap().put(R.id.layoutphoto,Arrays.asList(PhotoFragment.class));

        this.addClickListener();

        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(this));
    }

    /**
     * 添加事件
     */
    private void addClickListener(){
        TextView btnMe = findViewById(R.id.btnMe);
        ImageView imgMe = findViewById(R.id.imgMe);
        btnMe.setOnClickListener(new AddFragmentListener(new MeFragment(this),MenuUtils.Instance().getMap(),this));
        imgMe.setOnClickListener(new AddFragmentListener(new MeFragment(this),MenuUtils.Instance().getMap(),this));

        TextView textPhoto = findViewById(R.id.btnPhoto);
        ImageView imgPhoto = findViewById(R.id.imgPhoto);
        textPhoto.setOnClickListener(new AddFragmentListener(new PhotoFragment(this),MenuUtils.Instance().getMap(),this));
        imgPhoto.setOnClickListener(new AddFragmentListener(new PhotoFragment(this),MenuUtils.Instance().getMap(),this));

    }


    // 自定义异常处理器
    private class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

        private Context context;

        public MyUncaughtExceptionHandler(Context ctx){
            this.context = ctx;
        }

        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            // 在这里处理未捕获的异常，例如记录日志、显示错误信息等
            Log.e("MyApplication", "Uncaught exception: " + throwable.getMessage(), throwable);

            Toast.makeText(this.context,throwable.getMessage(),Toast.LENGTH_LONG);
        }
    }


}